package chapter10.section5;

//程序10-6：利用Files类实现文件拷贝、删除的例子
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class TestFiles {
	public static void main(String[] args) {
		try {
			Path source = Paths.get("d:/files/source.txt");
			System.out.println(source.getFileName());
			Path target = Paths.get("d:/files/backup.txt");
			// 拷贝文件，注意拷贝参数(如果存在，替换目标文件)
			Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
			Path toDel = Paths.get("d:/files/backup.txt");
			Files.delete(toDel);// 删除备份文件
			System.out.println("File deleted.");
			Path newDir = Paths.get("d:/files/newdir");
			Files.createDirectories(newDir);// 创建新目录
			// 将source.txt移到newdir目录中
			Files.move(source, newDir.resolve(source.getFileName()), StandardCopyOption.REPLACE_EXISTING);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}